package com.codi.bus.core.service.impl;

import com.codi.base.exception.ExceptionHandler;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.constant.RuleConstant;
import com.codi.bus.core.dao.LookupDao;
import com.codi.bus.core.domain.Lookup;
import com.codi.bus.core.domain.LookupKey;
import com.codi.bus.core.service.LookupService;
import com.codi.bus.exception.BaseException;
import com.codi.bus.exception.SystemDBException;
import com.codi.message.constant.ErrorConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("lookupService")
public class LookupServiceImpl implements LookupService {

    private static final Logger logger = LoggerFactory.getLogger(LookupServiceImpl.class);

    @Resource(name = "lookupDao")
    private LookupDao lookupDao;

    /**
     * 根据字典类型获取所有字典值
     *
     * @param lookupType
     * @return
     * @throws BaseException
     */
    @Override
    public List<Lookup> getLookups(String lookupType) throws BaseException {

        try {
            return lookupDao.getLookupByType(lookupType);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, "getLookups - Exception:", exception);
            throw new SystemDBException("getLookups Failure!", exception);
        }
    }

    /**
     * 根据字典类型获取企业宝
     *
     * @param lookupType
     * @return
     * @throws BaseException
     */
    @Override
    public List<Lookup> getOrganBabies(String lookupType) throws BaseException {
        try {
            return lookupDao.getOrganBabies(lookupType);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, "getLookups - Exception:", exception);
            throw new SystemDBException("getLookups Failure!", exception);
        }
    }

    @Override
    public Lookup getOrganBaby(String lookupType, String lookupValue) throws BaseException {
        try {
            LookupKey key = new LookupKey();
            key.setRuleType(lookupType);
            key.setRuleValue(lookupValue);
            return lookupDao.getOrganBaby(key);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, "getLookup - Exception:", exception);
            throw new SystemDBException("getLookup Failure!", exception);
        }
    }

    @Override
    public int deleteOrganBaby(String lookupType, String lookupValue) throws BaseException {
        try {
            LookupKey key = new LookupKey();
            key.setRuleType(lookupType);
            key.setRuleValue(lookupValue);
            return lookupDao.deleteLookupByKey(key);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, "getLookup - Exception:", exception);
            throw new SystemDBException("getLookup Failure!", exception);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Lookup getLookup(String lookupType) {

        List<Lookup> lookups = lookupDao.getLookupByType(lookupType);
        if (lookups == null || lookups.isEmpty()) {
            logger.error("没有查询到对应记录，请检查类型[{}]", lookupType);
            ExceptionHandler.publish(ErrorConst.COMMON_SYSTEM_ERROR);
        }
        // 只取一条记录
        return lookups.get(0);
    }

    /**
     * 根据字典类型，字典值获取字典项
     *
     * @param lookupType
     * @param lookupValue
     * @return
     * @throws BaseException
     */
    @Override
    public Lookup getLookup(String lookupType, String lookupValue) throws BaseException {
        try {
            LookupKey key = new LookupKey();
            key.setRuleType(lookupType);
            key.setRuleValue(lookupValue);
            return lookupDao.getLookupByKey(key);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, "getLookup - Exception:", exception);
            throw new SystemDBException("getLookup Failure!", exception);
        }
    }

    @Override
    public Boolean isT0Fund(String fundCode) {
        List<Lookup> T0Funds = lookupDao.getLookupByType(RuleConstant.RULE_LOOKUP_TYPE_T0FUND);


        if (CollectionUtils.isEmpty(T0Funds)) {
            return false;
        }
        for (Lookup lookup : T0Funds) {
            if (lookup.getRuleValue().equals(fundCode)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean updateLookup(Lookup lookup) {
        return lookupDao.updateByRuleTypeAndRuleValue(lookup);
    }

}
